import { createStore, applyMiddleware, compose } from 'redux';
import createSagaMiddleware from 'redux-saga'; // 引入redux-saga中的createSagaMiddleware函数
import logger from 'redux-logger'; // 引入saga.js
import modules from './modules';
// Logger with default options
const isDev = process.env.NODE_ENV === 'development';

const { reducers, sagas } = modules;

const devTools = isDev && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
const composeEnhancers = devTools || compose;
const sagaMiddleware = createSagaMiddleware();
const middlewares = [
  sagaMiddleware,
  logger, // Logger with default options
];

const configure = (preloadedState) => {
  const store = createStore(
    reducers,
    preloadedState,
    composeEnhancers(applyMiddleware(...middlewares)),
  );
  // 执行rootSaga
  sagas.map((item) => {
    return sagaMiddleware.run(item);
  });
  return store;
};

export default configure;
